import {
  MiddlewareConsumer,
  Module,
  NestModule,
  RequestMethod,
} from '@nestjs/common';
import { CacheModule } from '@nestjs/cache-manager';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersModule } from './users/users.module';
import { CaptchaModule } from './captcha/captcha.module';
// import { ConfigModule } from './config/config.module';
import { LoggerMiddleware } from './logger/logger.middleware';
import { UsersController } from './users/users.controller';
import { UploadModule } from './upload/upload.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ManageModule } from './manage/manage.module';
import { AuthModule } from './auth/auth.module';
import Constants from './common/constants';
import Config from './config/config';
import { JwtModule } from '@nestjs/jwt';
import { APP_GUARD } from '@nestjs/core';
import { RoleModule } from './role/role.module';
import { User } from './users/entities/user.entity';
import { Role } from './role/entities/role.entity';
import { PermissionModule } from './permission/permission.module';
import { Permission } from './permission/entities/permission.entity';
import { RoleGuard } from './role/role.guard';
import { ArticleModule } from './article/article.module';
import { SortModule } from './sort/sort.module';
import { TagModule } from './tag/tag.module';
import { CommentModule } from './comment/comment.module';
import { Article } from './article/entities/article.entity';
import { Comment } from './comment/entities/comment.entity';
import { Sort } from './sort/entities/sort.entity';
import { Tag } from './tag/entities/tag.entity';
import { ResourceModule } from './resource/resource.module';
import { OssModule } from './oss/oss.module';
import { RedisModule } from './redis/redis.module';
import { PermissionGuard } from './permission/permission.guard';

@Module({
  //   configModule就可以被userModule和captchaModule使用
  imports: [
    UsersModule,
    CaptchaModule,
    // ConfigModule,
    UploadModule,
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: Config.mysqlConfig.host,
      // logging: ['query'],
      port: +Config.mysqlConfig.port,
      username: Config.mysqlConfig.username,
      password: Config.mysqlConfig.password,
      database: Config.mysqlConfig.database,
      // entities: [__dirname + '/**/*.entity{.ts,.js}'],
      retryDelay: 500,
      retryAttempts: 3,
      autoLoadEntities: true,
      // synchronize: true,
    }),
    TypeOrmModule.forFeature([
      User,
      Role,
      Permission,
      Article,
      Comment,
      Sort,
      Tag,
    ]),
    ManageModule,
    AuthModule,
    CacheModule.register({ ttl: Constants.Redis_Ttl, isGlobal: true }),
    RoleModule,
    PermissionModule,
    ArticleModule,
    SortModule,
    TagModule,
    CommentModule,
    ResourceModule,
    OssModule,
    RedisModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'Test',
      useValue: ['a', 'b', 'c'],
    },
    {
      provide: APP_GUARD,
      useClass: RoleGuard,
    },
    {
      provide: APP_GUARD,
      useClass: PermissionGuard,
    },
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('*');
    // consumer.apply(LoggerMiddleware).forRoutes(UsersController)
    // consumer.apply(LoggerMiddleware).forRoutes({
    //     path: 'users',
    //     method: RequestMethod.GET,
    // })
  }
}
